package com.hjq.fcloud.ui.activity

import android.widget.ListView
import com.hjq.fcloud.R
import com.hjq.fcloud.app.AppActivity
import com.hjq.fcloud.bean.FlowDataBean
import com.hjq.fcloud.bean.FlowStatusBean
import com.hjq.fcloud.config.FlowStatusConfig
import com.hjq.fcloud.config.SystemSet
import com.hjq.fcloud.http.api.FlowListApi
import com.hjq.fcloud.http.api.FlowResetApi
import com.hjq.fcloud.http.api.FlowStatusApi
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.jump.CommonJumpBean
import com.hjq.fcloud.ui.adapter.FlowUnfinishedAdapter
import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallback
import okhttp3.Call

abstract class BaseDetailActivity : AppActivity() {

    companion object {
        const val INTENT_IN_DISABLE_AGAIN_SIGN = "enableAgainSign"//禁用“重新签约”
        const val INTENT_IN_ITEM = "item"
        const val IMAGE_MAX_SELECT = 50
    }

    val commonJumpBean by lazy { getSerializable<CommonJumpBean>(INTENT_IN_ITEM) }

    var flowDataBean: FlowDataBean? = null

    var flowStatusData: FlowStatusApi.FlowStatusData? = null

    //是否禁用“重新签约”，默认false 不禁用
    val isDisableAgainSign by lazy { getBoolean(INTENT_IN_DISABLE_AGAIN_SIGN) }

    /**
     * 是否有签约权限，true有
     */
    fun hasSignPermission(): Boolean {
        var hasPermission =
            (SystemSet.getMenuPermission(commonJumpBean?.originalBizCode)?.edit == 1)
        return hasPermission && commonJumpBean?.isStartSign == true
    }

    /**
     * 获取流程状态
     * canAgainSign 能重新签约
     * canSign 能签约
     * errorMessageArray 当前业务的前置流程未完成，展示提示页面的文本
     */
    fun getServerFlowStatus(callBack: (FlowStatusBean) -> Unit) {
        var parms = HashMap<String, Any?>()
        parms.put("fnc_id", commonJumpBean?.bizId)
        parms.put("biz_code", commonJumpBean?.bizCode)
        handlerFlowStatusRequestParms(parms)
        EasyHttp.post(this)
            .api(FlowStatusApi())
            .json(parms)
            .request(object : HttpCallback<HttpData<FlowStatusApi.FlowStatusData?>?>(this) {
                override fun onStart(call: Call?) {}
                override fun onSucceed(data: HttpData<FlowStatusApi.FlowStatusData?>?) {
                    flowStatusData = data?.getData()

                    var hasSignPermission = hasSignPermission()

                    var flowStatusBean = FlowStatusBean()
                    flowStatusBean.canSign =
                        FlowStatusConfig.canSign(flowStatusData) && hasSignPermission
                    flowStatusBean.canAgainSign =
                        FlowStatusConfig.canAgainSign(flowStatusData) && hasSignPermission
                    flowStatusBean.isFlowUnfinished =
                        FlowStatusConfig.isFlowUnfinished(flowStatusData)
                    flowStatusBean.flowUnfinishedTipsArray = flowStatusData?.curr_info
                    callBack(flowStatusBean)
                }
            })
    }


    /**
     * 获取流程
     */
    fun getServerFlow() {
        var parms = HashMap<String, Any?>()
        parms.put("biz_id", commonJumpBean?.bizId)
        parms.put("biz_code", commonJumpBean?.bizCode)
        handlerFlowRequestParms(parms)
        EasyHttp.post(this)
            .api(FlowListApi())
            .json(parms)
            .request(object : HttpCallback<HttpData<FlowDataBean?>?>(this) {
                override fun onStart(call: Call?) {}
                override fun onSucceed(data: HttpData<FlowDataBean?>?) {
                    flowDataBean = data?.getData()
                    setFlowViewData(flowDataBean)
                }
            })
    }


    open fun handlerFlowStatusRequestParms(parms: HashMap<String, Any?>) {

    }

    open fun handlerFlowRequestParms(parms: HashMap<String, Any?>) {

    }

    /**
     * 设置流程UI，交给子类实现
     */
    open fun setFlowViewData(data: FlowDataBean?) {

    }

    /**
     * 清除步骤（先调用清除步骤的接口，再调用通过/不通过的接口）
     */
    fun requestServerResetFlow(callBack: () -> Unit) {
        var api = FlowResetApi()

        var parms = HashMap<String, Any?>()
        parms.put("fnc_id", commonJumpBean?.bizId)
        parms.put("step", commonJumpBean?.bizCode)
        handlerFlowStatusRequestParms(parms)
        EasyHttp.post(this)
            .api(api)
            .json(parms)
            .request(object :
                HttpCallback<HttpData<Any?>?>(this) {
                override fun onSucceed(result: HttpData<Any?>?) {
                    callBack()
                }
            })
    }

    /**
     * 展示流程未完成view
     */
    fun showFlowUnfinishedView(flowUnfinishedTipsArray: Array<String>?) {
        //自定义EmptyView
        var flowUnfinishedView = layoutInflater.inflate(R.layout.layout_flow_unfinished, null)
        mLoadingAndRetryManager?.mLoadingAndRetryLayout?.setEmptyView(flowUnfinishedView)
        var flowUnfinishedLv = flowUnfinishedView.findViewById<ListView>(R.id.flowUnfinishedLv)
        var list = ArrayList<String>()
        flowUnfinishedTipsArray?.forEach { list.add(it) }
        flowUnfinishedLv.adapter = FlowUnfinishedAdapter(context, list)
        showAppEmpty()
    }
}